package com.yoo.ons.function.home

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.yoo.module_base.databinding.ItemCardBinding
import com.yoo.module_base.utils.log
import com.yoo.module_base.utils.setOnClickCallback
import com.yoo.module_base.view.CustomVerticalIndicator
import com.yoo.ons.adapter.HomeImageAdapter
import com.yoo.ons.base.adpater.AdapterCallback
import com.yoo.ons.model.HomeListModel
import com.yoo.ons.util.appContext
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.util.BannerUtils

@SuppressLint("ClickableViewAccessibility")
class HomeDataListViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback?,
    private val binding: ItemCardBinding = ItemCardBinding.inflate(
        LayoutInflater.from(parent.context),
        parent,
        false,
    )
) : RecyclerView.ViewHolder(binding.root) {

    init {
        binding.imgItemCardUnlike.setOnClickCallback {
            //点击不喜欢
            callback?.invoke(HOME_CARD_UNLIKE_CLICK, itemView)
        }
        binding.imgItemCardLike.setOnClickCallback {
            //点击喜欢
            callback?.invoke(HOME_CARD_LIKE_CLICK, itemView)
        }
        binding.conItemCardNickName.setOnClickCallback {
            //点击昵称
            callback?.invoke(HOME_CARD_NICKNAME_CLICK, itemView)
            //callback?.invoke(HOME_CARD_NICKNAME_CLICK,(itemView.tag as HomeListModel.MatchIndexBean).images[binding.itemUserBanner.currentItem])
        }
        binding.imgItemCardFlash.setOnClickCallback {
            //fc
            callback?.invoke(HOME_CARD_FLASH_CHAT_CLICK, itemView)
        }
    }

    @SuppressLint("SetTextI18n")
    fun bind(item: HomeListModel.MatchIndexBean, position: Int) {
        itemView.tag = Pair(item, position)
        val linearLineIndicator = CustomVerticalIndicator(appContext)
        binding.txtItemCardNickName.text = "${item.nickName},${item.age}"
        (item.interests.map { it.interest }).joinToString(separator = ", ")
            .also { binding.txtItemCardInterest.text = it }


        if (item.images.isNotEmpty()) {
            if (item.images.size > 1) {
                binding.itemUserBanner.setUserInputEnabled(false)
                linearLineIndicator.setIndicatorCount(item.images.size)
                binding.itemUserBanner.indicator = linearLineIndicator
                binding.itemUserBanner.setIndicatorMargins(
                    IndicatorConfig.Margins(
                        0, 0, BannerUtils.dp2px(20f).toInt(), 0
                    )
                )
            }
            binding.itemUserBanner.setAdapter(HomeImageAdapter(appContext, item.images))
            binding.itemUserBanner.viewPager2.registerOnPageChangeCallback(object :
                ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    linearLineIndicator.setCurrentIndex(position)
                    super.onPageSelected(position)
                }
            })
        }
    }

    fun setBannerCurrentItem(touchType: Int) {
        if (touchType == 0) {
            //up 下一个
            if (binding.itemUserBanner.itemCount > 1)
                binding.itemUserBanner.currentItem += 1
        } else {
            if (binding.itemUserBanner.itemCount > 1)
                binding.itemUserBanner.currentItem -= 1
        }
    }

    fun hideCardContentUI(isShow:Boolean){
        binding.txtItemCardNickName.visibility = if(isShow) View.VISIBLE else View.GONE
        binding.txtItemCardInterest.visibility = if(isShow) View.VISIBLE else View.GONE
        binding.conItemCardBtn.visibility = if(isShow) View.VISIBLE else View.GONE
    }
}